package com.huizhonglm.diap.core.storage.db;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.core.storage.model.UserInfo;

import org.xutils.ex.DbException;

import java.util.List;

/**
 * 数据库业务类
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-01 16:44
 */
public class DBMethod {

    private static DBMethod mInstance;

    /**
     * 获取单例
     *
     * @return DBMethod
     */
    public static DBMethod getInstance() {
        if (mInstance == null) {
            synchronized (DBMethod.class) {
                if (mInstance == null) {
                    mInstance = new DBMethod();
                }
            }
        }
        return mInstance;
    }

    private DBMethod() {
    }

    public boolean saveUserInfo(UserInfo info) {
        if (info != null) {
            try {
                List<UserInfo> list = DBHelper.getInstance().db.selector(UserInfo.class).where("userId", "in", new String[]{info.getUserId()}).findAll();
                if (!EmptyUtil.isEmpty(list)) {
                    DBHelper.getInstance().db.delete(list);
                }
                DBHelper.getInstance().db.save(info);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public UserInfo findUserInfo() {
        try {
            return DBHelper.getInstance().db.selector(UserInfo.class).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteUserInfo(){
        try {
            UserInfo info = findUserInfo();
            if (info != null) {
                List<UserInfo> list = DBHelper.getInstance().db.selector(UserInfo.class).where("userId", "in", new String[]{info.getUserId()}).findAll();
                if (!EmptyUtil.isEmpty(list)) {
                    DBHelper.getInstance().db.delete(list);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public boolean saveLocationInfo(LocationInfo info){
//        if (info != null) {
//            try {
//                List<LocationInfo> list = DBHelper.getInstance().db.selector(LocationInfo.class).where("cityCode", "in", new String[]{info.getCityCode()}).findAll();
//                if (!EmptyUtil.isEmpty(list)) {
//                    DBHelper.getInstance().db.delete(list);
//                }
//                DBHelper.getInstance().db.save(info);
//                return true;
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return false;
//    }
//
//    public LocationInfo findLocationInfo(){
//        try {
//            return DBHelper.getInstance().db.selector(LocationInfo.class).findFirst();
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
